package com.sy.service.purchase.impl;

import com.sy.bean.purchase.ChuRuDetail;
import com.sy.bean.purchase.ChuStock;
import com.sy.bean.purchase.RuStock;
import com.sy.common.crud.BaseServiceImpl;
import com.sy.mapper.purchase.ChuRuDetailMapper;
import com.sy.mapper.purchase.ChuRuStockMapper;
import com.sy.service.purchase.ChuRuStockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;

@Service
public class ChuRuStockServiceImpl extends BaseServiceImpl<ChuStock, ChuRuStockMapper> implements ChuRuStockService {

    String currentMaxCode; //当前最大编码 CK2024001
    @Autowired
    ChuRuStockMapper chuRuStockMapper;

    @Autowired
    ChuRuDetailMapper chuRuDetailMapper;
    @Override
    public List<RuStock> queryRuStock(Map map) {
        return chuRuStockMapper.queryRuStock(map);
    }

    @Transactional
    @Override
    public int insert(RuStock ruStock) {
        ruStock.setCode(generateCode());
        ruStock.setType(2);
        int rs=chuRuStockMapper.insert(ruStock);
        if (rs==0)
            throw new RuntimeException("保存入货单失败");
        List<ChuRuDetail>list=ruStock.getChuRuDetailList();
        list.forEach(o->{
            o.setStatus(2);
        });
        rs=chuRuDetailMapper.batchSave(ruStock.getId(),list);
        if (rs==0)
            throw new RuntimeException("保存入货单明细失败");
        return 1;
    }

    /**
     * 保存退货出库单
     * @param entity 要保存的对象
     * @return
     */
    @Transactional
    @Override
    public int save(ChuStock entity) {
        entity.setCode(generateCode());
        entity.setType(1);
        int rs=super.save(entity);
        if (rs==0)
            throw new RuntimeException("保存出货单失败");
        List<ChuRuDetail>list=entity.getChuRuDetailList();
        list.forEach(o->{
            o.setStatus(1);
        });
        rs=chuRuDetailMapper.batchSave(entity.getId(),list);
        if (rs==0)
            throw new RuntimeException("保存出货明细失败");
        return 1;
    }



     String generateCode() {
        String year = getYear();
        //判断当前最大编码是否存在
        if (null == currentMaxCode) {
            //如果不存在，查询当年最大编码
            currentMaxCode = baseMapper.queryMaxCode(year);
            if (null == currentMaxCode) {
                //说明不存在，生产第一个编码
                currentMaxCode = "CK" + year + "001";
            } else {
                //如果存在，新编码序号+1
                getNextCode();
            }
        } else {
            //如果存在，新编码序号+1
            getNextCode();
        }
        return currentMaxCode;
    }

    String generateCode1() {
        String year = getYear();
        //判断当前最大编码是否存在
        if (null == currentMaxCode) {
            //如果不存在，查询当年最大编码
            currentMaxCode = baseMapper.queryMaxCode(year);
            if (null == currentMaxCode) {
                //说明不存在，生产第一个编码
                currentMaxCode = "RK" + year + "001";
            } else {
                //如果存在，新编码序号+1
                getNextCode();
            }
        } else {
            //如果存在，新编码序号+1
            getNextCode();
        }
        return currentMaxCode;
    }
    void getNextCode() {
        //CG2024001
        String prefix = currentMaxCode.substring(0, 6);
        String seq = currentMaxCode.substring(6);
        //转换数字001. tmp=1
        int tmp = Integer.parseInt(seq);
        tmp++;
        currentMaxCode = prefix + String.format("%03d", tmp);
    }

    String getYear() {
        LocalDate localDate = LocalDate.now();
        return String.valueOf(localDate.getYear());
    }
}
